package com.fh.controller;

import com.fh.common.ResultEnum;
import com.fh.common.ResultMassage;
import com.fh.model.Area;
import com.fh.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zhangh
 * @create 2020-07-17 14:15
 */
@Controller
@RequestMapping("AreaController")
public class AreaController {

    @Autowired
    private AreaService  areaService;



    @RequestMapping("toShow")
    public String   toShow(){
        return "area/area-show";
    }

    @RequestMapping("selectArea")
    @ResponseBody
    public ResultMassage selectArea(){
        try {
          List<Area> listArea=areaService.selectArea();
          return  new ResultMassage(ResultEnum.QUERY_SUCCESS_MSG,listArea);
        }catch (Exception e){

          return  new ResultMassage(ResultEnum.QUERY_ERROR_MSG);
        }
    }

    @RequestMapping("addArea")
    @ResponseBody
    public ResultMassage addArea(Area area){
        try {
            areaService.addArea(area);
            return new ResultMassage(ResultEnum.ADD_SUCCESS_MSG);
        }catch (Exception e){
            return new ResultMassage(ResultEnum.ADD_ERROR_MSG);
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public ResultMassage toUpdate(Integer areaId){
        try {
            Area area=areaService.selectAreaId(areaId);
            return new ResultMassage(ResultEnum.TOUPDATE_SUCCESS_MSG,area);
        }catch (Exception e){
            return new ResultMassage(ResultEnum.TOUPDATE_ERROR_MSG);
        }
    }

    @RequestMapping("updateArea")
    @ResponseBody
    public ResultMassage updateArea(Area area){
        try {
            areaService.updateArea(area);
            return new ResultMassage(ResultEnum.UPDATE_SUCCESS_MSG);
        }catch (Exception e){
            return new ResultMassage(ResultEnum.UPDATE_ERROR_MSG);
        }
    }

    @RequestMapping("deleteArea")
    @ResponseBody
    public  ResultMassage deleteArea(@RequestParam("areaId[]") List<Integer> areaId){
        try {
            areaService.deleteArea(areaId);
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }catch (Exception e){
            return new ResultMassage(ResultEnum.DELETE_ERROR_MSG);
        }

    }
}
